All files / src/app/api/items/[id]/restore route.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                      5x 5x 5x 4x   4x 4x   1x 1x   1x      
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { ItemController } from "@/server/api/items";
import { requireApiItemCapability } from "@/server/dal/collection-access";
import { getDb } from "@/lib/db";
import { errorResponse } from "@/server/api/http-errors";
 
export async function POST(
  _request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const db = getDb();
    // items:write = curator. includeDeleted: true — the item to restore IS soft-deleted.
    const item = await requireApiItemCapability(db, id, user.id, "items:write", { includeDeleted: true });
    if (item instanceof NextResponse) return item;
 
    const restored = await ItemController.fromDb(db).restoreItem(id);
    return NextResponse.json(restored);
  } catch (error) {
    return errorResponse(error);
  }
}